#!/usr/bin/python
import os, pygame
import guifaimage, predef
from pygame.locals import *
from guifaimage import *
from predef import *

class PlayerIndicator:
    def __init__(self, x, y, w, h, img):
        self.PW_BIG = 50
        self.PH_BIG = 50
        self.PW_SMALL = 30
        self.PH_SMALL = 30
        self.rect = Rect(x, y, w, h)
        self.rect_big = [] + \
                [Rect(x + w/2 - self.PW_BIG/2, y + h - self.PH_BIG, self.PW_BIG, self.PH_BIG)] + \
                [Rect(x + w - self.PW_BIG, y + h/2 - self.PH_BIG/2, self.PW_BIG, self.PH_BIG)] + \
                [Rect(x + w/2 - self.PW_BIG/2, y, self.PW_BIG, self.PH_BIG)] + \
                [Rect(x, y + h/2 - self.PH_BIG/2, self.PW_BIG, self.PH_BIG)]

        self.rect_small = [] + \
        [Rect(x + w/2 - self.PW_SMALL/2, y + h - self.PH_SMALL, self.PW_SMALL, self.PH_SMALL)] + \
        [Rect(x + w - self.PW_SMALL, y + h/2 - self.PH_SMALL/2, self.PW_SMALL, self.PH_SMALL)] + \
        [Rect(x + w/2 - self.PW_SMALL/2, y, self.PW_SMALL, self.PH_SMALL)] + \
        [Rect(x, y + h/2 - self.PH_SMALL/2, self.PW_SMALL, self.PH_SMALL)]

        self.image_big = []
        for i in range(0, len(img)):
            self.image_big.append(pygame.transform.scale(img[i], (self.PW_BIG, self.PH_BIG)))
        self.image_small = []
        for i in range(0, len(img)):
            self.image_small.append(pygame.transform.scale(img[i], (self.PW_SMALL, self.PH_SMALL)))
        self.player = {
                PLAYER1:1,
                PLAYER2:1,
                PLAYER3:1,
                PLAYER4:1
                }
        self.curplayer = PLAYER1
        self.playerlist = (PLAYER1, PLAYER2, PLAYER3, PLAYER4)

    def set_player(self, p):
        if self.player[p] == 1:
            self.curplayer = p

    def get_current_player(self):
        return self.curplayer

    def disable_player(self, p):
        self.player[p] = 0

    def draw(self, screen):
        pygame.draw.rect(screen, (0,0,0), self.rect)
        i = 0
        j = 0
        for p in self.playerlist:
            if self.player[p] == 1:
                if self.curplayer == p:
                    j = i
                else:
                    screen.blit(self.image_small[i], self.rect_small[i])
            i = i+1
        screen.blit(self.image_big[j], self.rect_big[j])

    def on_mouse_click(self, (x, y)):
        if self.rect.collidepoint(x, y):
            i = 0
            for r in self.rect_big:
                if r.collidepoint(x, y):
                    return self.playerlist[i]
                i = i + 1
        return None

class CorpseSelector:
    def __init__(self, x, y, chesslist=[]):
        self.chesslist = chesslist
        w = len(chesslist) * CHESSSIZE
        self.rect = Rect(x, y, w, CHESSSIZE)

    def _resize(self):
        for c in self.chesslist:
            pass

    def set_pos(self, x, y):
        self.rect[0] = x
        self.rect[1] = y

    def get_width(self):
        return len(self.chesslist) * CHESSSIZE

    def append_chess(self, chess):
        if chess.type != TYPE_CORPSE:
            raise TypeError
        self.chesslist.append(chess)

    def draw(self, screen):
        pygame.draw.rect(screen, (0,128,128), self.rect)
        i = 0
        for c in self.chesslist:
            screen.blit(c.image, Rect(self.rect[0]+i, self.rect[1], CHESSSIZE, CHESSSIZE))
            i = i + CHESSSIZE

    def on_mouse_click(self, (x, y)):
        if self.rect.collidepoint(x, y):
            i = CHESSSIZE
            for c in self.chesslist:
                if x < self.rect[0] + i:
                    return c
                i = i + CHESSSIZE
        else:
            return None

class SkillNode:
    def __init__(self, skill):
        self.skilltype = skill
        self.enable = False
        self.show = False
        self.img_enable = None
        self.img_disable = None

    def draw(self, screen, x, y):
        if self.enable:
            screen.blit(self.img_enable, 
                    Rect(x, y, self.img_enable.get_rect()[2], 
                    self.img_enable.get_rect()[3]))
        else:
            screen.blit(self.img_disable, 
                    Rect(x, y, self.img_disable.get_rect()[2], 
                    self.img_disable.get_rect()[3]))

class SkillSelector:
    def __init__(self, x, y, w, h):
        self.rect = Rect(x, y, w, h)
        self.abilitylist = {}

    def append_node(self, skillnode):
        self.abilitylist[skillnode.skilltype] = skillnode

    def reset(self):
        for k, c in self.abilitylist.iteritems():
            c.show = False
            c.enable = False

    def set_show(self, skill, b):
        self.abilitylist[skill].show = b

    def set_enable(self, skill, b):
        self.abilitylist[skill].enable = b

    def draw(self, screen):
        pygame.draw.rect(screen, (0,0,0), self.rect)
        x = self.rect[0]
        y = self.rect[1]
        for k, c in self.abilitylist.iteritems():
            if c.show:
                c.draw(screen, x, y)
                y = y + CHESSSIZE + 5

    def on_mouse_click(self, (x, y)):
        if self.rect.collidepoint(x, y):
            x0 = self.rect[0]
            y0 = self.rect[1]
            noderect = Rect(x0, y0, CHESSSIZE, CHESSSIZE)
            for k, c in self.abilitylist.iteritems():
                if c.show:
                    if noderect.collidepoint(x, y):
                        if c.enable:
                            return c.skilltype
                        else:
                            return None
                    else:
                        noderect[1] = noderect[1] + CHESSSIZE + 5
            return None
        else:
            return None

class Button:
    def __init__(self, x, y, w, h, image):
        self.rect = Rect(x, y, w, h)
        self.image = image
        self.display = False

    def hide(self):
        self.display = False

    def show(self):
        self.display = True

    def draw(self, screen):
        if not self.display:
            return
        pygame.draw.rect(screen, (0,0,0), self.rect)
        screen.blit(self.image, self.rect)

    def on_mouse_click(self, (x, y)):
        if self.rect.collidepoint(x, y):
            return True
        else:
            return False
